package com.chlitina.store.modules.cash.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.cash.model.Member;
import com.chlitina.store.modules.cash.model.recharge.CardAllRecharge;
import com.chlitina.store.modules.cash.model.recharge.CardDurationRecharge;
import com.chlitina.store.modules.cash.model.recharge.CardProductRecharge;
import com.chlitina.store.modules.cash.model.recharge.CardTimesRecharge;
import com.chlitina.store.modules.cash.model.recharge.RechargeElementManager;
import com.chlitina.store.modules.cash.model.recharge.RehcargeHost;
import com.chlitina.store.modules.employee.dao.EmployeeDao;
import com.chlitina.store.modules.employee.entity.Employee;

/**
 * recharge Controller
 * 
 * @author Songsong.xu
 * @version 2013-12-02
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH + "/cash/member/recharge")
public class RechargeController extends BaseController {
	public static interface KEY {
		String KEY_MEMBER = "member";
		String KEY_TAB_ACTIVE = "tabActive";
		String MESSGAE = "message";
	}

	@Autowired
	private EmployeeDao employeeDao;
	
	public static interface URI {
		String TIMES_LIST = "modules/cash/rechargeCardTimesList";
		String TIMES_FORM = "modules/cash/rechargeCardTimesForm";
		String ALL_LIST = "modules/cash/rechargeCardAllList";
		String ALL_FORM = "modules/cash/rechargeCardAllForm";
		String PRODUCT_LIST = "modules/cash/rechargeCardProductList";
		String PRODUCT_FORM = "modules/cash/rechargeCardProductForm";
		String DURATION_LIST = "modules/cash/rechargeCardDurationList";
		String DURATION_FORM = "modules/cash/rechargeCardDurationForm";
	}

	private Member getAndRegisterMember(HttpServletRequest request, Model model) {
		Member member = (Member) request.getSession().getAttribute(
				KEY.KEY_MEMBER);
		model.addAttribute(KEY.KEY_MEMBER, member);
		return member;
	}

	@RequestMapping(value = { "cardall" })
	public String cardAllList(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Member member = getAndRegisterMember(request, model);
		return URI.ALL_LIST;
	}

	@RequestMapping(value = { "cardproduct" })
	public String cardProductList(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Member member = getAndRegisterMember(request, model);
		return URI.PRODUCT_LIST;
	}

	@RequestMapping(value = { "cardproduct/form" })
	public String cardProductForm(@RequestParam("card") String uuid,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Member member = getAndRegisterMember(request, model);

		RechargeElementManager rechargeEleMgr = member.getRechargeEleMgr();
		RehcargeHost element = rechargeEleMgr.findByUUID(
				rechargeEleMgr.getProductCards(), Integer.parseInt(uuid));
		rechargeEleMgr.setCurrentElement(element);
		return URI.PRODUCT_FORM;
	}

	@RequestMapping(value = { "cardproduct/recharge" })
	public String cardProductRecharge(@RequestParam("uuid") String uuid,@RequestParam("employeeId") String employeeId,
			@RequestParam("rechargeMoney") String rechargeMoney,
			@RequestParam("costMoney") String costMoney,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Member member = getAndRegisterMember(request, model);
		RechargeElementManager rechargeEleMgr = member.getRechargeEleMgr();
		int uid = Integer.parseInt(uuid);
		long eID = Long.parseLong(employeeId);
		Employee employee= employeeDao.findOne(eID);
		if(employee==null){
			model.addAttribute(KEY.MESSGAE, "状态错误，请重新对卡进行充值卡");
			return URI.PRODUCT_LIST;
		}
		member.setEmployee(employee);
		
		RehcargeHost element = rechargeEleMgr.findByUUID(
				rechargeEleMgr.getProductCards(), uid);
		if (rechargeEleMgr.getCurrentElement() != element) {
			model.addAttribute(KEY.MESSGAE, "状态错误，请重新对卡进行充值卡");
			return URI.PRODUCT_LIST;
		}
		List<RehcargeHost> rechargedList = rechargeEleMgr
				.getRechargedCardProduct();
		if (rechargedList == null) {
			rechargeEleMgr
					.setRechargedCardProduct(rechargedList = new ArrayList<RehcargeHost>());
		}
		RehcargeHost host = rechargeEleMgr.findByUUID(rechargedList, uid);
		if (host != null) {
			//
		}
		float rechargeMY = 0;
		float costMY = 0;
		try {
			rechargeMY = Float.parseFloat(rechargeMoney);
		} catch (NumberFormatException e) {
		}

		try {
			costMY = Float.parseFloat(costMoney);
		} catch (NumberFormatException e) {
		}
		if (rechargeMY == 0) {
			model.addAttribute(KEY.MESSGAE, "错误的充值金额");
			return cardProductForm(uuid, request, response, model);
		}
		if (costMY == 0) {
			model.addAttribute(KEY.MESSGAE, "错误的支付金额");
			return cardProductForm(uuid, request, response, model);
		}

		CardProductRecharge recharge = new CardProductRecharge();
		recharge.setEmployee(employee);
		recharge.setRechargeMoney(rechargeMY);
		recharge.setMoney(costMY);
		element.getRecharges().add(recharge);
		if (!rechargedList.contains(element)) {
			rechargedList.add(element);
		}
		model.addAttribute(KEY.MESSGAE, "充值已记录到购物车");

		member.updateMenuState();
		return URI.PRODUCT_LIST;
	}

	@RequestMapping(value = { "cardall/form" })
	public String cardAllForm(@RequestParam("card") String uuid,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Member member = getAndRegisterMember(request, model);

		RechargeElementManager rechargeEleMgr = member.getRechargeEleMgr();
		RehcargeHost element = rechargeEleMgr.findByUUID(
				rechargeEleMgr.getAllCards(), Integer.parseInt(uuid));
		rechargeEleMgr.setCurrentElement(element);
		return URI.ALL_FORM;
	}

	@RequestMapping(value = { "cardall/recharge" })
	public String cardAllRecharge(@RequestParam("uuid") String uuid,@RequestParam("employeeId") String employeeId,
			@RequestParam("rechargeMoney") String rechargeMoney,
			@RequestParam("costMoney") String costMoney,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Member member = getAndRegisterMember(request, model);
		RechargeElementManager rechargeEleMgr = member.getRechargeEleMgr();
		int uid = Integer.parseInt(uuid);
		long eID = Long.parseLong(employeeId);
		Employee employee= employeeDao.findOne(eID);
		if(employee==null){
			model.addAttribute(KEY.MESSGAE, "状态错误，请重新对卡进行充值卡");
			return URI.PRODUCT_LIST;
		}
		member.setEmployee(employee);
		
		RehcargeHost element = rechargeEleMgr.findByUUID(
				rechargeEleMgr.getAllCards(), uid);
		if (rechargeEleMgr.getCurrentElement() != element) {
			model.addAttribute(KEY.MESSGAE, "状态错误，请重新对卡进行充值卡");
			return URI.ALL_LIST;
		}
		List<RehcargeHost> rechargedList = rechargeEleMgr.getRechargedCardAll();
		if (rechargedList == null) {
			rechargeEleMgr
					.setRechargedCardAll(rechargedList = new ArrayList<RehcargeHost>());
		}
		RehcargeHost host = rechargeEleMgr.findByUUID(rechargedList, uid);
		if (host != null) {
			// has recharged once
		}
	
		float rechargeMY = 0;
		try {
			rechargeMY = Float.parseFloat(rechargeMoney);
		} catch (NumberFormatException e) {
		}
		float costMY = 0;
		try {
			costMY = Float.parseFloat(costMoney);
		} catch (NumberFormatException e) {
		}
		if (rechargeMY == 0) {
			model.addAttribute(KEY.MESSGAE, "错误的充值金额");
			return cardAllForm(uuid, request, response, model);
		}
		if (costMY == 0) {
			model.addAttribute(KEY.MESSGAE, "错误的支付金额");
			return cardAllForm(uuid, request, response, model);
		}
		CardAllRecharge recharge = new CardAllRecharge();
		recharge.setEmployee(employee);
		recharge.setRechargeMoney(rechargeMY);
		recharge.setMoney(costMY);
		element.getRecharges().add(recharge);
		if (!rechargedList.contains(element)) {
			rechargedList.add(element);
		}
		model.addAttribute(KEY.MESSGAE, "充值已记录到购物车");
		member.updateMenuState();
		return URI.ALL_LIST;
	}

	@RequestMapping(value = { "cardtimes" })
	public String cardTimesList(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		getAndRegisterMember(request, model);
		return URI.TIMES_LIST;
	}

	@RequestMapping(value = { "cardtimes/form" })
	public String cardTimesForm(@RequestParam("card") String uuid,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Member member = getAndRegisterMember(request, model);

		RechargeElementManager rechargeEleMgr = member.getRechargeEleMgr();
		RehcargeHost element = rechargeEleMgr.findByUUID(
				rechargeEleMgr.getTimesCards(), Integer.parseInt(uuid));
		rechargeEleMgr.setCurrentElement(element);
		return URI.TIMES_FORM;
	}

	@RequestMapping(value = { "cardtimes/recharge" })
	public String cardTimesRecharge(@RequestParam("uuid") String uuid,@RequestParam("employeeId") String employeeId,
			@RequestParam("rechargeTimes") String rechargeTimes,
			@RequestParam("costMoney") String costMoney,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Member member = getAndRegisterMember(request, model);
		RechargeElementManager rechargeEleMgr = member.getRechargeEleMgr();
		int uid = Integer.parseInt(uuid);
		long eID = Long.parseLong(employeeId);
		Employee employee= employeeDao.findOne(eID);
		if(employee==null){
			model.addAttribute(KEY.MESSGAE, "状态错误，请重新对卡进行充值卡");
			return URI.PRODUCT_LIST;
		}
		member.setEmployee(employee);
		
		RehcargeHost element = rechargeEleMgr.findByUUID(
				rechargeEleMgr.getTimesCards(), uid);
		if (rechargeEleMgr.getCurrentElement() != element) {
			model.addAttribute(KEY.MESSGAE, "状态错误，请重新对卡进行充值卡");
			return URI.TIMES_LIST;
		}
		List<RehcargeHost> rechargedList = rechargeEleMgr
				.getRechargedCardTimes();
		if (rechargedList == null) {
			rechargeEleMgr
					.setRechargedCardTimes(rechargedList = new ArrayList<RehcargeHost>());
		}
		RehcargeHost host = rechargeEleMgr.findByUUID(rechargedList, uid);
		if (host != null) {
			//
		}
		int times = 0;
		float costMY = 0;
		try {
			times = Integer.parseInt(rechargeTimes);
		} catch (NumberFormatException e) {
		}

		try {
			costMY = Float.parseFloat(costMoney);
		} catch (NumberFormatException e) {
		}
		if (times == 0) {
			model.addAttribute(KEY.MESSGAE, "错误的充值次数");
			return cardTimesForm(uuid, request, response, model);
		}
		if (costMY == 0) {
			model.addAttribute(KEY.MESSGAE, "错误的支付金额");
			return cardTimesForm(uuid, request, response, model);
		}

		CardTimesRecharge recharge = new CardTimesRecharge();
		recharge.setEmployee(employee);
		recharge.setTimes(times);
		recharge.setMoney(costMY);
		element.getRecharges().add(recharge);
		if (!rechargedList.contains(element)) {
			rechargedList.add(element);
		}
		model.addAttribute(KEY.MESSGAE, "充值已记录到购物车");
		member.updateMenuState();
		return URI.TIMES_LIST;
	}

	@RequestMapping(value = { "cardduration" })
	public String cardDurationList(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Member member = getAndRegisterMember(request, model);

		RechargeElementManager rechargeEleMgr = member.getRechargeEleMgr();
		rechargeEleMgr.init(member);
		return URI.DURATION_LIST;
	}

	@RequestMapping(value = { "cardduration/form" })
	public String cardDurationForm(@RequestParam("card") String uuid,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Member member = getAndRegisterMember(request, model);

		RechargeElementManager rechargeEleMgr = member.getRechargeEleMgr();
		RehcargeHost element = rechargeEleMgr.findByUUID(
				rechargeEleMgr.getDurationCards(), Integer.parseInt(uuid));
		rechargeEleMgr.setCurrentElement(element);
		return URI.DURATION_FORM;
	}

	@RequestMapping(value = { "cardduration/recharge" })
	public String cardDurationRecharge(@RequestParam("uuid") String uuid,@RequestParam("employeeId") String employeeId,
			@RequestParam("rechargeDuration") String rechargeDuration,
			@RequestParam("costMoney") String costMoney,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Member member = getAndRegisterMember(request, model);
		RechargeElementManager rechargeEleMgr = member.getRechargeEleMgr();
		int uid = Integer.parseInt(uuid);
		long eID = Long.parseLong(employeeId);
		Employee employee= employeeDao.findOne(eID);
		if(employee==null){
			model.addAttribute(KEY.MESSGAE, "状态错误，请重新对卡进行充值卡");
			return URI.PRODUCT_LIST;
		}
		member.setEmployee(employee);
		RehcargeHost element = rechargeEleMgr.findByUUID(
				rechargeEleMgr.getDurationCards(), uid);
		if (rechargeEleMgr.getCurrentElement() != element) {
			model.addAttribute(KEY.MESSGAE, "状态错误，请重新对卡进行充值卡");
			return URI.DURATION_LIST;
		}

		List<RehcargeHost> rechargedList = rechargeEleMgr
				.getRechargedCardDuration();
		if (rechargedList == null) {
			rechargeEleMgr
					.setRechargedCardDuration(rechargedList = new ArrayList<RehcargeHost>());
		}
		RehcargeHost host = rechargeEleMgr.findByUUID(rechargedList, uid);
		if (host != null) {
			//
		}
		int duration = 0;
		float costMY = 0;
		try {
			duration = Integer.parseInt(rechargeDuration);
		} catch (NumberFormatException e) {
		}

		try {
			costMY = Float.parseFloat(costMoney);
		} catch (NumberFormatException e) {
		}
		if (duration == 0) {
			model.addAttribute(KEY.MESSGAE, "错误的充值时段");
			return cardDurationForm(uuid, request, response, model);
		}
		if (costMY == 0) {
			model.addAttribute(KEY.MESSGAE, "错误的支付金额");
			return cardDurationForm(uuid, request, response, model);
		}
		CardDurationRecharge recharge = new CardDurationRecharge();
		recharge.setEmployee(employee);
		recharge.setDuration(duration);
		recharge.setMoney(costMY);
		element.getRecharges().add(recharge);
		if (!rechargedList.contains(element)) {
			rechargedList.add(element);
		}
		model.addAttribute(KEY.MESSGAE, "充值已记录到购物车");

		member.updateMenuState();
		return URI.DURATION_LIST;
	}
}
